//------页面加载 事件绑定 start------- 
_global_number = 1;
(function() {
	//自建CheckBox事件绑定
	$(".u-chenkbox").click(function() {
		var $div = $(this).find("div");
		if ($div.hasClass("u-chenkbox-animation-two")) {
			$div.removeClass("u-chenkbox-animation-two");
			$div.addClass("u-chenkbox-animation-one");
			$(this).find("input[type='hidden']").val(0);
		} else {
			$div.removeClass("u-chenkbox-animation-one");
			$div.addClass("u-chenkbox-animation-two");
			$(this).find("input[type='hidden']").val(1);
		}
	});
	
	/*tab键处理*/
	$("textarea").keypress(function(event) {
		if (event.keyCode == 9) {
			this.value = this.value.substring(0, this.selectionStart) + "\t" + this.value.substring(this.selectionStart, this.value.length);
		}
	});
})();
//------页面加载 事件绑定 end------- 




//--------------行号处理 start---------------------------
function lineNumber() {
	var start = Date.now();
	var text = $(".u-editor-textarea").val(),
		totaLinNum = 1; //总行数 默认1
	if ( text && text != "" ) {
		/*
		 * 总行数等于换行符总个数 
		 */
		var enter = text.match(/\r|\n/g);
		if (enter) {
			totaLinNum = enter.length + 1;
		}
	} else {
		text = "";
	}
	console.log("total time 1 : " + (Date.now()- start));
	start = Date.now();
	if (_global_number == totaLinNum) {
		return;
	}
	
	_global_number = totaLinNum;
	
	/* 
	 * 获取光标位置
	 * 根据光标位置，统计光标之后的文字换行符个数
	 * changNum 光标所在行
	 */
	var cursorStart = $(".u-editor-textarea")[0].selectionStart,
		changNum = text.substring(0, cursorStart + 1).match(/\r|\n/g);
	changNum = changNum ? changNum.length : 1;
	
	//console.log("changNum " + changNum);

	var	$numDiv = $(".line-number-div > div"),
		$nums = $numDiv.find("span"),
		max = totaLinNum > $nums.length ? totaLinNum : $nums.length,
		numHtml = new Array(max - $nums.length); //新增行数
		//复制文字处理
		changNum = $nums.length < changNum ? $nums.length : changNum;
	
	console.log("total time 2 : " + (Date.now()- start));
	start = Date.now();
	
	//处理变动行
	for ( var i = changNum; i <= $nums.length; i++ ) {
		$($nums[i-1]).text(i + ".");
	}
	
	//处理新增行
	for ( var i = $nums.length + 1; i <= totaLinNum; i++ ) {
		numHtml.push("<span>");
		numHtml.push(i);
		numHtml.push(".</span>");
	}
	if ( numHtml.length > 0 ) {
		$numDiv.append(numHtml.join(""));
	}
	
	console.log("total time 3 : " + (Date.now()- start));
	start = Date.now();
	//删除多余行
	if ( $nums.length > totaLinNum ) {
		$($nums.splice(totaLinNum)).remove();
	}
	console.log("total time 4 : " + (Date.now()- start));
}

//页码与textarea滚动条绑定
$(".u-editor-textarea").scroll(function(){
    $(".line-number-div > div").css("margin-top", $(this).scrollTop() * -1 + "px"); 
});
//--------------行号处理 end---------------------------



//----------------------编辑功能处理 start-------------------------------
String.prototype.replaceAll = function (t, c) {
	var str = this;
    while( str.indexOf(t) >= 0 ) {
		str = str.replace(t, c);
	}
    return str;
}

function replaceText() {
	var $source = $("#source-ta"),
		$target = $("#replace-target"),
		$content = $("#replace-content");
		
	if ($target.val() == "" || $source.val() == "") {
		return; 
	}
	
	//是否使用正则表达式
	var regular = !$("input[name='u-chenkbox-regular']").val(),
		dispose = function(str) {
			if ( regular ) {
				return str.replace( eval( $target.val() ), $content.val());
			} else {
				return str.replaceAll($target.val(), $content.val());
			}
		};
	
	//是否单行操作
	if ($("input[name='u-chenkbox-line']").val() == 1) {
			$source.val(dispose($source.val()));
	} else {
		var srcArr = $source.val().split("\n"),
			newSrcArr = new Array();
		$.each(srcArr, function(i, str) {
			newSrcArr.push(dispose(str));
		});
		$source.val( newSrcArr.join("\n") );
	}
}

function joinText() {
	var $source = $("#source-ta"),
		first = $("#join-first").val(),
		last = $("#join-last").val();
	var texts = $source.val().split("\n"),
		str = new Array();
	$.each(texts, function(i, v) {
		str.push(first);
		str.push(v);
		str.push(last);
		str.push("\n");
	});
	$source.val(str.join(""));
}
//----------------------编辑功能处理 end-------------------------------